use rstest::rstest;

use super::super::*;
use crate::tests::common::{assert_expected_result_or_error, lock_serial_test};
use crate::tests::fixtures::test_data;

#[rstest]
fn test_tile_type_raw_initialization(
    #[values(TileType::Raster, TileType::Text, TileType::Svg)] tile_type: TileType,
) {
    match tile_type {
        // The following match will fail if the variant is missing or is not a unit variant.
        TileType::Raster | TileType::Text | TileType::Svg => (),
    }
}

#[rstest]
#[case::valid_raster_file_path_red_white_dds(
    PathBuf::from(test_data("four_raster_image_tiles/some/other/red-white.dds",)),
    Some(TileType::Raster),
    None
)]
#[case::valid_raster_file_path_gray_jpg(
    PathBuf::from(test_data("four_raster_image_tiles/gray.jpg",)),
    Some(TileType::Raster),
    None
)]
#[case::valid_text_file_path_gray_jpg(
    PathBuf::from(test_data("single_text_tile/ink",)),
    Some(TileType::Text),
    None
)]
#[case::invalid_raster_file_tile_arithmetic_jpg(
    PathBuf::from(test_data("text_tile_two_rows_three_cols_invalid")),
    None,
    Some(Error::ValueError(format!(
        "tile type could not be determined from the file {}",
        test_data("text_tile_two_rows_three_cols_invalid")
    )))
)]
#[case::invalid_raster_file_tile_arithmetic_jpg(
    PathBuf::from(test_data("tile-invalid.jpg")),
    None,
    Some(Error::ValueError(format!(
        "tile type could not be determined from the file {}",
        test_data("tile-invalid.jpg")
    )))
)]
fn test_tile_type_determine_by_file_path(
    #[case] file_path: PathBuf,
    #[case] expected_tile_type: Option<TileType>,
    #[case] expected_error: Option<Error>,
) {
    // All tests that have log messages as side effect must be run in serial.
    let _guard = lock_serial_test();

    let result = TileType::determine_by_file_path(&file_path);

    assert_expected_result_or_error(result, expected_tile_type, expected_error);
}
